Dynamics Array Exercise

Public and Private Arrays


In [5]:
class M(object):
    def public(self):
        print 'This can be seen in tab'
        
    def _private(self):
        print 'This cant be seen in tab due to _'
        print '_ is a weak internel use indicator'

In [6]:
m = M()

In [7]:
m.public()


This can be seen in tab

In [8]:
m._private()


This cant be seen in tab due to _
_ is a weak internel use indicator

In [9]:
class MyClass():
    def __init__(self):
        self.__superprivate = 'Hello'
        self._semiprivate = 'World'

In [16]:
m = MyClass()

In [17]:
print m.__superprivate


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-9c0a67e739f2> in <module>()
----> 1 print m.__superprivate

AttributeError: MyClass instance has no attribute '__superprivate'

In [18]:
print m._semiprivate


World

In [19]:
print m.__dict__


{'_MyClass__superprivate': 'Hello', '_semiprivate': 'World'}

Create dynamic array


In [36]:
import ctypes

In [53]:
class DynamicArray(object):
    def __init__(self):
        self.n = 0
        self.capacity = 1
        self.A = self._make_array(self.capacity)
    
    def __len__(self):
        return self.n

    def __getitem__(self,k):
        if not 0 <= k <= self.n:
            return IndexError('k is out of bounds')
        
        return self.A[k]
    
    def _resize(self,new_cap):
        B = self._make_array(new_cap)
        
        for k in range(self.n):
            B[k] = self.A[k]
        self.A = B
        self.capacity = new_cap
    
    def append(self,ele):
        if self.n == self.capacity:
            self._resize(2*self.capacity)
        
        self.A[self.n] = ele
        self.n += 1
        
    def _make_array(self, new_cap):
        return (new_cap*ctypes.py_object)()

In [54]:
print range(10)


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [70]:
da = DynamicArray()

In [71]:
len(da)


Out[71]:
0

In [72]:
da.capacity


Out[72]:
1

In [57]:
da.append(1)

In [58]:
len(da)


Out[58]:
1

In [59]:
da[0]


Out[59]:
1

In [60]:
da[1]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-60-d2db4a350d16> in <module>()
----> 1 da[1]

<ipython-input-53-db039a5ea96a> in __getitem__(self, k)
     12             return IndexError('k is out of bounds')
     13 
---> 14         return self.A[k]
     15 
     16     def _resize(self,new_cap):

IndexError: invalid index

In [61]:
da.append(2)

In [62]:
da[1]


Out[62]:
2

In [63]:
import sys

In [75]:
da = DynamicArray()
for k in range(50):
    a = len(da)
    #b = sys.getsizeof(da)
    b = da.capacity
    print 'Length: {0:3d}; Size in bytes {1:4d}'.format(a,b) 
    da.append(k)


Length:   0; Size in bytes    1
Length:   1; Size in bytes    1
Length:   2; Size in bytes    2
Length:   3; Size in bytes    4
Length:   4; Size in bytes    4
Length:   5; Size in bytes    8
Length:   6; Size in bytes    8
Length:   7; Size in bytes    8
Length:   8; Size in bytes    8
Length:   9; Size in bytes   16
Length:  10; Size in bytes   16
Length:  11; Size in bytes   16
Length:  12; Size in bytes   16
Length:  13; Size in bytes   16
Length:  14; Size in bytes   16
Length:  15; Size in bytes   16
Length:  16; Size in bytes   16
Length:  17; Size in bytes   32
Length:  18; Size in bytes   32
Length:  19; Size in bytes   32
Length:  20; Size in bytes   32
Length:  21; Size in bytes   32
Length:  22; Size in bytes   32
Length:  23; Size in bytes   32
Length:  24; Size in bytes   32
Length:  25; Size in bytes   32
Length:  26; Size in bytes   32
Length:  27; Size in bytes   32
Length:  28; Size in bytes   32
Length:  29; Size in bytes   32
Length:  30; Size in bytes   32
Length:  31; Size in bytes   32
Length:  32; Size in bytes   32
Length:  33; Size in bytes   64
Length:  34; Size in bytes   64
Length:  35; Size in bytes   64
Length:  36; Size in bytes   64
Length:  37; Size in bytes   64
Length:  38; Size in bytes   64
Length:  39; Size in bytes   64
Length:  40; Size in bytes   64
Length:  41; Size in bytes   64
Length:  42; Size in bytes   64
Length:  43; Size in bytes   64
Length:  44; Size in bytes   64
Length:  45; Size in bytes   64
Length:  46; Size in bytes   64
Length:  47; Size in bytes   64
Length:  48; Size in bytes   64
Length:  49; Size in bytes   64